package com.dds.system.service.union.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dds.common.utils.DateUtils;
import com.dds.system.domain.search.union.UnionShopOrderSearch;
import com.dds.system.mapper.app.AppUserScoreDetailMapper;
import com.dds.system.mapper.union.UnionProductOrderMapper;
import com.dds.system.mapper.union.UnionShopOrderMapper;
import com.dds.system.mapper.union.UnionShopOrderProfitMapper;
import com.dds.system.mapper.union.UnionShopOrderRefundMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.domain.union.UnionShopOrder;
import com.dds.system.service.union.IUnionShopOrderService;

/**
 * 联盟商订单Service业务层处理
 *
 * @author gaolianjie
 * @date 2021-04-01
 */
@Service
public class UnionShopOrderServiceImpl implements IUnionShopOrderService {
    @Autowired
    private UnionShopOrderMapper unionShopOrderMapper;

    @Autowired
    private UnionProductOrderMapper unionProductOrderMapper;

    @Autowired
    private AppUserScoreDetailMapper appUserScoreDetailMapper;

    @Autowired
    private UnionShopOrderProfitMapper unionShopOrderProfitMapper;

    @Autowired
    private UnionShopOrderRefundMapper unionShopOrderRefundMapper;

    /**
     * 查询联盟商订单
     *
     * @param orderId 联盟商订单ID
     * @return 联盟商订单
     */
    @Override
    public UnionShopOrder selectUnionShopOrderById(Integer orderId) {
        return unionShopOrderMapper.selectUnionShopOrderById(orderId);
    }

    /**
     * 查询联盟商订单列表
     *
     * @return 联盟商订单
     */
    @Override
    public List<Map<String, Object>> selectUnionShopOrderList(UnionShopOrderSearch unionShopOrderSearch) {
        return unionShopOrderMapper.selectUnionShopOrderList(unionShopOrderSearch);
    }

    /**
     * 新增联盟商订单
     *
     * @param unionShopOrder 联盟商订单
     * @return 结果
     */
    @Override
    public int insertUnionShopOrder(UnionShopOrder unionShopOrder) {
        return unionShopOrderMapper.insertUnionShopOrder(unionShopOrder);
    }

    /**
     * 修改联盟商订单
     *
     * @param unionShopOrder 联盟商订单
     * @return 结果
     */
    @Override
    public int updateUnionShopOrder(UnionShopOrder unionShopOrder) {
        return unionShopOrderMapper.updateUnionShopOrder(unionShopOrder);
    }

    /**
     * 批量删除联盟商订单
     *
     * @param orderIds 需要删除的联盟商订单ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopOrderByIds(Integer[] orderIds) {
        return unionShopOrderMapper.deleteUnionShopOrderByIds(orderIds);
    }

    /**
     * 删除联盟商订单信息
     *
     * @param orderId 联盟商订单ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopOrderById(Integer orderId) {
        return unionShopOrderMapper.deleteUnionShopOrderById(orderId);
    }

    /**
     * 获取首页订单概览数据
     * @param selectDay 日期
     * @return
     */
    @Override
    public Map<String, Object> getOrderData(String selectDay) {
        Map<String, Object> returnData = new HashMap<>();
        String startTime = null, endTime = null;
        switch (selectDay) {
            case "1":
                startTime = DateUtils.getDate();
                endTime = DateUtils.getDate();
                break;
            case "2":
                startTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.addDays(new Date(), -1));
                endTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.addDays(new Date(), -1));
                break;
            case "7":
                startTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.getBeginDayOfWeek());
                endTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.getEndDayOfWeek());
                break;
            case "30":
                startTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.getBeginDayOfMonth());
                endTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.getEndDayOfMonth());
                break;
            case "90":
                startTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.addDays(new Date(), -90));
                endTime = new SimpleDateFormat(DateUtils.YYYY_MM_DD).format(DateUtils.addDays(new Date(), 0));
                break;
            default:
        }

        Map<String, Object> orderData = unionShopOrderMapper.getOrderData(startTime, endTime);
        Map<String, Object> refundData = unionShopOrderMapper.getRefundData(startTime, endTime);
        Map<String, Object> scoreOrderData = unionProductOrderMapper.getScoreData(startTime, endTime);
        double scoreChange = appUserScoreDetailMapper.getData("2", "2", startTime, endTime);
        double scoreAdd = appUserScoreDetailMapper.getData("1", "1", startTime, endTime);
        double scoreCeil = appUserScoreDetailMapper.getData("3", "1", startTime, endTime);
        Map<String, Object> addProfit = unionShopOrderProfitMapper.getData("1", startTime, endTime);
        Map<String, Object> ceilProfit = unionShopOrderProfitMapper.getData("2", startTime, endTime);
        Map<String, BigDecimal> addChannelFee = unionShopOrderMapper.getData(startTime, endTime);
        Map<String, Object> ceilChannelFee = unionShopOrderRefundMapper.getData(startTime, endTime);


        BigDecimal orderAmount = new BigDecimal(orderData.get("amount").toString());
        BigDecimal orderCount = new BigDecimal(orderData.get("count").toString());

        BigDecimal orderAverage = new BigDecimal(0);
        if ((long)orderData.get("count") != 0) {
            orderAverage = orderAmount.divide(orderCount, 2, BigDecimal.ROUND_DOWN);
        }

        returnData.put("orderAmount", orderAmount);
        returnData.put("orderCount", orderCount);
        returnData.put("orderAverage", orderAverage);

        BigDecimal refundAmount = new BigDecimal(refundData.get("amount").toString());
        BigDecimal refundCount = new BigDecimal(refundData.get("count").toString());
        returnData.put("refundAmount", refundAmount);
        returnData.put("refundCount", refundCount);
        returnData.put("refundScore", refundData.get("score"));

        returnData.put("scoreOrder", scoreOrderData.get("count"));
        returnData.put("scoreChange", scoreChange);
        returnData.put("scorePay", scoreAdd - scoreCeil);

        BigDecimal addPlatform = new BigDecimal(addProfit.get("platform").toString());
        BigDecimal ceilPlatform = new BigDecimal(ceilProfit.get("platform").toString());
        BigDecimal addOperationProfit = new BigDecimal(addProfit.get("operation_profit").toString());
        BigDecimal ceilOperationProfit = new BigDecimal(ceilProfit.get("operation_profit").toString());
        returnData.put("platform", addPlatform.subtract(ceilPlatform));
        returnData.put("operationProfit", addOperationProfit.subtract(ceilOperationProfit));

        BigDecimal addMsf = new BigDecimal(addChannelFee.get("msf").toString());
        BigDecimal addDds = new BigDecimal(addChannelFee.get("dds").toString());
        BigDecimal ceilMsf = new BigDecimal(ceilChannelFee.get("msf").toString());
        BigDecimal ceilDds = new BigDecimal(ceilChannelFee.get("dds").toString());
        returnData.put("msfChannelFee", addMsf.subtract(ceilMsf));
        returnData.put("ddsChannelFee", addDds.subtract(ceilDds));

        return returnData;
    }
}
